package com.itjiange.config;

import com.itjiange.constant.CommonConstant;
import com.itjiange.socket.SocketReceive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Socket 服务端
 *
 * @author ItJiange
 * @date 2023/11/3
 */
@Slf4j
//@Configuration
public class SockerServerConfig {

    @Value("${myServer.rootPath}")
    private String rootPath;
    private ServerSocket serverSocket = null;
    @Bean
    public void serverSocketApplication() {
        try {
            log.info("socket 服务端启动");
            serverSocket = new ServerSocket(CommonConstant.PORT);
            while (true) {
                Socket socket = serverSocket.accept();
                log.info("接收到客户端socket" + socket.getRemoteSocketAddress());
                new SocketReceive(socket, rootPath).receiveService();
            }
        } catch (IOException e) {
            log.error(e.getMessage());
        }
    }
}
